﻿using iMES.Core.Extensions;
using iMES.Core.Utilities;
using iMES.Custom.IRepositories;
using iMES.Entity.DomainModels;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZJY.Purchase.IRepositroies;

namespace ZJY.Purchase.Services
{
    public partial class Purchase_supplierServices
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IPurchase_supplierRepository _repository;//访问数据库
        private readonly IBase_NumberRuleRepository _numberRuleRepository;//自定义编码规则访问数据库
        [ActivatorUtilitiesConstructor]
        public Purchase_supplierServices(IHttpContextAccessor httpContextAccessor, IPurchase_supplierRepository repository, IBase_NumberRuleRepository numberRuleRepository) : base(repository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = repository;
            _numberRuleRepository = numberRuleRepository;
        }
        WebResponseContent webResponse = new WebResponseContent();
        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="saveDataModel"></param>
        /// <returns></returns>
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            //此处saveModel是从前台提交的原生数据，可对数据进修改过滤
            AddOnExecuting = (Purchase_supplier tool, object list) =>
            {
                
                //如果返回false,后面代码不会再执行
                if (repository.Exists(x => x.supplier_name == tool.supplier_name))
                {
                    return webResponse.Error("供应商名称已存在");
                }
                return webResponse.OK();
            };
            return base.Add(saveDataModel);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="saveDataModel"></param>
        /// <returns></returns>
        public override WebResponseContent Update(SaveModel saveDataModel)
        {
            //此处saveModel是从前台提交的原生数据，可对数据进修改过滤
            AddOnExecuting = (Purchase_supplier tool, object list) =>
            {
                //如果返回false,后面代码不会再执行
                if (repository.Exists(x => x.supplier_name == tool.supplier_name))
                {
                    return webResponse.Error("供应商名称已存在");
                }
                return webResponse.OK();
            };
            return base.Update(saveDataModel);
        }

    }
}
